package com.shopManager;


import com.netflix.client.ClientException;
import feign.RetryableException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import shopBean.common.Exception.MyException;
import shopBean.common.bean.RestResult;
import shopBean.common.responseCode.Status;
import shopBean.common.utils.RestResultGenerator;
import shopBean.common.utils.UUIDGenerator;

@ControllerAdvice
public class RestExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(RestExceptionHandler.class);

    @ExceptionHandler
    @ResponseBody
    private <T> RestResult<T> myExceptionExceptionHandler(Exception e) {

        String uuid = UUIDGenerator.generate38();
        log.error("errorid:" + uuid);
        log.error("---------> huge error!", e);
        if (StringUtils.isBlank(e.getMessage()) ||( e.getCause()!=null && e.getCause() instanceof ClientException)) {
            return RestResultGenerator.genErrorResult("系统异常", uuid, Status.sys_error);
        }

        if (e instanceof MyException) {
            return RestResultGenerator.genErrorResult(e.getMessage(), uuid, ((MyException) e).getErrorCode());
        }

        return RestResultGenerator.genErrorResult(e.getMessage(), uuid, Status.sys_error);
    }


}
